package org.ccay.security.listener;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.ccay.core.dispatcher.EventUtil;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.exception.ExceptionHandler;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.core.session.ISessionExpirationHanlder;
import org.ccay.core.session.ISessionStartHanlder;
import org.ccay.core.session.ISessionStopHanlder;
import org.ccay.core.session.SessionEventArgs;

public class DefaultSessionListener implements SessionListener{

	private static final ILogger logger = CcayLoggerFactory.getLogger(DefaultSessionListener.class); 
	@Override
	public void onStart(Session session) {
		logger.debug("session created:"+session.getId());
		try {
			EventUtil.dispatch(ISessionStartHanlder.class, this,new SessionEventArgs(new SessionAdapter(session)), false);
		} catch (ApplicationException e) {
			ExceptionHandler.logException(e);
		}
	}

	@Override
	public void onStop(Session session) {
		logger.debug("session stoped:"+session.getId());
		try {
			EventUtil.dispatch(ISessionStopHanlder.class, this,new SessionEventArgs(new SessionAdapter(session)), false);
		} catch (ApplicationException e) {
			ExceptionHandler.logException(e);
		}
		
	}

	@Override
	public void onExpiration(Session session) {
		logger.debug("session expired:"+session.getId());
		try {
			EventUtil.dispatch(ISessionExpirationHanlder.class, this,new SessionEventArgs(new SessionAdapter(session)), false);
		} catch (ApplicationException e) {
			ExceptionHandler.logException(e);
		}
	}

}
